﻿
#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2013-02-22
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012-2013 ajayumi 版权所有
'————————————————————————————————————————
#End Region


Imports NLog
Imports NLogger = NLog.Logger
Imports NLog.LayoutRenderers

''' <summary>
''' 日志等级
''' </summary>
''' <remarks></remarks>
Public Enum LogLevel
    ''' <summary>
    ''' 信息类型的消息
    ''' </summary>
    Normal
    ''' <summary>
    ''' 最常见的记录信息，一般用于普通输出
    ''' </summary>
    Trace
    ''' <summary>
    ''' 警告信息，一般用于比较重要的场合 
    ''' </summary>
    Warn
    ''' <summary>
    ''' 错误信息
    ''' </summary>
    [Error]
    ''' <summary>
    ''' 致命异常信息。一般来讲，发生致命异常之后程序将无法继续执行。
    ''' </summary>
    Fatal
End Enum

''' <summary>
''' 日志类
''' </summary>
''' <remarks></remarks>
Public Class Logger

    'private static readonly Lazy<AuthCompanyManager> current = new Lazy<AuthCompanyManager>(() => new AuthCompanyManager(), true);
    Private Shared ReadOnly current As New Lazy(Of Logger)(Function()
                                                               Return New Logger()
                                                           End Function, True)

#Region "Variables"
    Protected Shared m_Logger As NLogger = LogManager.GetCurrentClassLogger()
#End Region

#Region "Properties"
    Private Shared m_Instance As Logger
    Public Shared ReadOnly Property Instance() As Logger
        Get
            'If m_Instance Is Nothing Then
            '    m_Instance = New Logger()
            'End If
            'Return m_Instance
            Return current.Value
        End Get
    End Property

#End Region

#Region "Constructors"

    Protected Sub New()

    End Sub

#End Region

#Region "Methods"
    Public Sub Writer(msg As String)
        Me.Writer(LogLevel.Normal, msg)
    End Sub

    ''' <summary>
    ''' 记录日志
    ''' </summary>
    ''' <param name="level"></param>
    ''' <param name="msg"></param>
    ''' <remarks></remarks>
    Public Sub Writer(level As LogLevel, msg As String)
        Select Case level
            Case LogLevel.Normal
                m_Logger.Info(msg)
                Exit Select
            Case LogLevel.Trace
                m_Logger.Trace(msg)
                Exit Select
            Case LogLevel.Warn
                m_Logger.Warn(msg)
                Exit Select
            Case LogLevel.Error
                m_Logger.Error(msg)
                Exit Select
            Case LogLevel.Fatal
                m_Logger.Fatal(msg)
                Exit Select
        End Select

    End Sub
#End Region




End Class

''' <summary>
''' 自定义消息显示布局
''' </summary>
''' <remarks></remarks>
<LayoutRenderer("customMessage")>
Public Class CustomMessageLayoutRenderer
    Inherits NLog.LayoutRenderers.MessageLayoutRenderer

    'Public Property Message As String = String.Empty

    Protected Overrides Sub Append(builder As System.Text.StringBuilder, logEvent As NLog.LogEventInfo)
        builder.Append(Now.ToString("yyyy-MM-dd HH:mm:ss"))
        builder.AppendLine()
        MyBase.Append(builder, logEvent)
    End Sub
End Class